/*
The code examples copyrighted by Nokia Corporation that are included to
this material are licensed to you under following MIT-style License:

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <QGraphicsLinearLayout>

#include <MApplication>
#include <MApplicationWindow>
#include <MApplicationPage>
#include <MLinearLayoutPolicy>
#include <MLayout>
#include <MLabel>
#include <MButton>


#include "orientationpage.hpp"



OrientationPage::OrientationPage(){
	// Create a MLayout attached to this page
	QGraphicsWidget *panel = centralWidget();
	MLayout *layout = new MLayout(panel);

	// Create two layout policies for each of two possible orientations
	// landscape and portrait.
	MLinearLayoutPolicy *landscapePolicy = new MLinearLayoutPolicy(layout, Qt::Vertical);
	MLinearLayoutPolicy *portraitPolicy = new MLinearLayoutPolicy(layout, Qt::Vertical);

	// set layout policies for every screen orientation
	layout->setPortraitPolicy(portraitPolicy);
	layout->setLandscapePolicy(landscapePolicy);

	// Set initial orientation state indication to the label
	this->onOrientationChangeFinished(MApplication::activeApplicationWindow()->orientation());

	// Connect signals of application window instance to receive notifications
	// of screen orientation change
	connect(
			MApplication::activeApplicationWindow(),
			SIGNAL(orientationChanged(M::Orientation)),
			this,
			SLOT(onOrientationChangeStarted(M::Orientation))
		);
	connect(
			MApplication::activeApplicationWindow(),
			SIGNAL(orientationChangeFinished(M::Orientation)),
			this,
			SLOT(onOrientationChangeFinished(M::Orientation))
		);


	// Set up different widget sets for each screen orientation mode

	{
		// landscape mode widgets set up
		landscapePolicy->addItem(&this->curOrientLabel);

		MWidget *landscapeWidget = new MWidget(this);
		landscapePolicy->addItem(landscapeWidget);

		QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal);
		landscapeWidget->setLayout(layout);

		MButton *helloButton = new MButton("Hello", landscapeWidget);
		layout->addItem(helloButton);

		MButton *worldButton = new MButton("World", landscapeWidget);
		layout->addItem(worldButton);
	}

	{
		// portrait mode widgets set up
		portraitPolicy->addItem(&this->curOrientLabel);

		MButton *helloButton = new MButton("Hello", this);
		portraitPolicy->addItem(helloButton);

		MButton *worldButton = new MButton("World", this);
		portraitPolicy->addItem(worldButton);
	}
}

void OrientationPage::onOrientationChangeFinished(M::Orientation orient){
	switch(orient){
		case M::Portrait:
			this->curOrientLabel.setText("Portrait orientation mode");
			break;
		default:
		case M::Landscape:
			this->curOrientLabel.setText("Landscape orientation mode");
			break;
	}
}



void OrientationPage::onOrientationChangeStarted(M::Orientation){
	this->curOrientLabel.setText("Rotating...");
}

